<template>
  <dic style="background-color:#fff;padding:10px 20px;">
    <sapn style="line-height:60px;font-size:20px;">设备信息管理</sapn>
    <el-button @click="goBack" type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </dic>
  <el-card style="margin:5px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="设备名称">
        <el-input  v-model="searchEqpt.name" placeholder="请输入" @keydown.enter.prevent="loadEqpt"></el-input>
      </el-form-item>
      <el-form-item label="设备厂商">
        <el-input v-model="searchEqpt.manufacturer" placeholder="请输入" @keydown.enter.prevent="loadEqpt"></el-input>
      </el-form-item>
      <el-form-item label="设备状态">
        <el-select v-model="searchEqpt.status" placeholder="请选择" @keydown.enter.prevent="loadEqpt" style="width: 100px">
          <el-option v-for="item in equStatusArr" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button @click="loadEqpt" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:5px;">
    <div style="margin:10px 0px 40px 10px;">
      <span style="font-weight: bold;">{{eqptTypeName}}</span>类的设备
      <el-button @click="addEqpt" type="primary" style="float: right;margin-top:-3px;">新增设备</el-button>
    </div>
    <el-table :data="eqptArr" style="width: 100%">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="设备名称" align="center"></el-table-column>
      <el-table-column prop="code" label="设备编号" align="center"/>
      <el-table-column prop="status" label="设备状态" align="center" :formatter="equStatusFormatter"/>
      <el-table-column prop="entryTime" label="入库时间" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" align="center"/>
      <el-table-column prop="scrapTime" label="报废时间" align="center"/>
      <el-table-column prop="price" label="购入价格" align="center"/>
      <el-table-column prop="manufacturer" label="设备厂商" align="center"/>
      <el-table-column prop="remark" label="备注" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editeEqpt(scope.row)">编辑</el-button>
          <el-button type="primary" link @click="delEqpt(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 30px;float:right;">
      <el-pagination
          layout="total,sizes,prev,pager,next,jumper"
          :page-sizes="[5,10,15,20]"
          v-model:current-page="searchEqpt.pageNum"
          v-model:page-size="searchEqpt.pageSize"
          v-model:total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </el-card>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :close-on-click-modal="false">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
       <el-col :span="12">
         <el-form-item label="设备名称">
           <el-input placeholder="请输入" v-model="eqpt.name"></el-input>
         </el-form-item>
       </el-col>
       <el-col :span="12">
         <el-form-item label="设备编号">
           <el-input placeholder="请输入" v-model="eqpt.code"></el-input>
         </el-form-item>
       </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="状态">
            <el-select placeholder="请输入" v-model="eqpt.status">
              <el-option v-for="item in equStatusArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报废时间">
            <el-date-picker  v-model="eqpt.scrapTime" placeholder="请选择" value-format="YYYY-MM-DD HH:mm:ss" type="datetime"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12"  v-if="showType">
          <el-form-item label="设备类别">
            <el-select placeholder="请选择" v-model="eqpt.eqptTypeId">
              <el-option v-for="item in eqptType" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="价格">
            <el-input placeholder="请输入" v-model="eqpt.price"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="厂商">
            <el-input placeholder="请输入" v-model="eqpt.manufacturer"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input placeholder="请输入" v-model="eqpt.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveEqpt">保存</el-button>
    </template>
  </el-dialog>

</template>

<script setup>
import router from "@/router";
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";

const eqptTypeId=new URLSearchParams(window.location.search).get("id")
const eqptTypeName=new URLSearchParams(window.location.search).get("name")
const goBack = () => {
  router.push('/empHome/equipmentType')
}
const searchEqpt=ref({pageSize:10,pageNum:1})
const eqptArr=ref([])
const total=ref()
const equStatusArr=ref([])
const dialogVisible=ref(false)
const dialogTitle=ref()
const eqpt=ref({})
const showType=ref(false)
const loadEqpt = () => {
  searchEqpt.value.eqptTypeId=eqptTypeId
  axios.post(BASE_URL+'/v1/equipment/select',searchEqpt.value)
      .then((response)=>{
        if (response.data.code==2000){
          eqptArr.value=response.data.data.equipmentVOS
          total.value=response.data.data.total
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
const eqptType=ref([])
const loadEqptType = () => {
  axios.post(BASE_URL+'/v1/equipmentType/select',{})
      .then((response)=>{
        if (response.data.code==2000){
          eqptType.value=response.data.data
          console.log(eqptType.value)
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
onMounted(()=>{
  loadEqpt(),loadEqptType(),loadDictOptions(equStatusArr,'EquipStatus')
})
const equStatusFormatter=(row,column,cellValue,index)=>{
  if (!equStatusArr){
    return cellValue;
  }
  for (let item of equStatusArr.value){
    if (item.value == cellValue){
      cellValue = item.label
    }
  }
  return cellValue;
}
const reset = () => {
  searchEqpt.value={}
  loadEqpt()
}
const handleSizeChange = (val) => {
  searchEqpt.value.pageSize=val
  loadEqpt()
}
const handleCurrentChange = (val) => {
  searchEqpt.value.pageNum=val
  loadEqpt()
}
const addEqpt = () => {
  dialogVisible.value=true
  dialogTitle.value="新增设备信息"
  eqpt.value={eqptTypeId:eqptTypeId}
  showType.value=false
}
const editeEqpt = (row) => {
  loadEqpt()
  eqpt.value=row
  dialogVisible.value=true
  dialogTitle.value="编辑设备信息"
  showType.value=true
}
const saveEqpt = () => {
  if (confirm("确认保存吗")){
    axios.post(BASE_URL+'/v1/equipment/save',eqpt.value)
        .then((response)=>{
          if (response.data.code==2000){
            dialogVisible.value=false
            ElMessage.success("保存成功")
            loadEqpt()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const delEqpt = (id) => {
  if (confirm("确认要删除吗")){
    axios.delete(BASE_URL+'/v1/equipment/delete/'+id)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("删除成功")
            loadEqpt()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const handleClose = () => {
  if (confirm("确认关闭窗口吗")){
    dialogVisible.value=false
  }
}
</script>

<style scoped>

</style>